//
//  BaseNetWorkManager.swift
//  民生生活
//
//  Created by apple on 2017/8/7.
//  Copyright © 2017年 youjindi.app.com. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

class BaseNetWorkManager: AFHTTPSessionManager {

    static let shared = BaseNetWorkManager()
    
    func request(urlString: String, parameters: [String: AnyObject]?, completion: @escaping (AnyObject?) -> ()) {
       
        let url = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
        
        let success = { (task: URLSessionDataTask, response: Any)->() in
            SVProgressHUD.dismiss()
            
            let result = String(data: response as! Data, encoding: String.Encoding.utf8)
            let jsonData = result?.data(using: String.Encoding.utf8)
            let data = (jsonData! as NSData).objectFromJSONData()
            if data != nil{
                completion(data as AnyObject?)
            }else{
                completion(response as AnyObject?)
            }

        }
        
        let failure = { (task: URLSessionDataTask?, error: Error) -> () in
            print("网络请求错误 \(error)")
        }
        
        SVProgressHUD.show()
        requestSerializer = AFHTTPRequestSerializer()
        responseSerializer = AFHTTPResponseSerializer()
        get(url!, parameters: parameters, progress: nil, success: success, failure: failure)
        
                
    }
}
